<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <input type="text" id="inp">
  <p id="demo"></p>
  <script>
    // const obj = { a: 1, b: 2 }
    // console.log(obj)

    // let obj = new Object
    // obj.name = '张学友'
    // obj.say = function () {}

    // let obj = {}
    // Object.defineProperty(obj, 'name', {
    //   get: function () {
    //     console.log('我被获取了')
    //     return val
    //   },
    //   set: function (newVal) {
    //     console.log('我被设置了')
    //   }
    // })

    // obj.name = '张学友'
    // let val = obj.name

    let obj = {}
    let val = obj.name
    let demo = document.querySelector('#demo')
    let inp = document.querySelector('#inp')

    Object.defineProperty(obj, 'name', {
      get: function () {
        return val
      },
      set: function (newVal) {
        inp.value = newVal
        demo.innerHTML = newVal
      }
    })

    inp.addEventListener('input', function (e) {
      // console.log(e.target.value)
      obj.name = e.target.value
    })

    obj.name = '张学友'
  </script>
</body>
</html>